package ru.urfc.portal.developer_tasks.ejb_tests;

import javax.ejb.EJB;

import ru.urfc.portal.developer_tasks.BaseTestEjb;
import ru.urfc.portal.developer_tasks.Initializer;
import ru.urfc.portal.managers.ClientManager;
import ru.urfc.portal.model.AssociationKey;
import ru.urfc.portal.model.AssociationKeyWithEmail;
import ru.urfc.portal.model.Client;
import ru.urfc.portal.model.admins.User;
import ru.urfc.portal.model.admins.UserCreate;

public class ClientManagerTestEjb extends BaseTestEjb {

	@EJB ClientManager clientManager;
	
	public void testGenerateKey() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_QQ);
		assertEquals(null, oneOrNull(find(AssociationKey.class, "client.id", client.getId())));
		AssociationKey key = clientManager.generateKey(client.getId());
		assertEquals(key.getId(), exactlyOne(find(AssociationKey.class, "client.id", client.getId())).getId());
		clientManager.generateKey(client.getId());
		exactlyOne(find(AssociationKey.class, "client.id", client.getId())).getId();
	}
	
	public void testKeyIsTransient() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		AssociationKey key = clientManager.generateKey(client.getId());
		assertNotNull(key.getKey());
		detach(client);
		detach(key);
		AssociationKey foundKey = exactlyOne(find(AssociationKey.class, "client.id", client.getId()));
		assertEquals(null, foundKey.getKey());
		assertNotNull(foundKey.getHash());
	}

	public void testGeneratedKeysIsDifferent() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		AssociationKey key1 = clientManager.generateKey(client.getId());
		AssociationKey key2 = clientManager.generateKey(client.getId());
		assertNotSame(key1, key2);
		assertEquals(false, key1.getKey().equals(key2.getKey()));
		assertEquals(false, key1.getHash().equals(key2.getHash()));
	}
	
	public void testAssociateOk() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		AssociationKey key = clientManager.generateKey(client.getId());
		assertNotNull(oneOrNull(find(AssociationKey.class, "hash", key.getHash())));
		String email = "some@mail";
		User user = clientManager.createUser(client.getId(), new UserCreate(email));
		assertEquals(CODE_USER_ROLE_USER, user.getRole().getCode());
		assertEquals(CODE_USER_STATE_INVITED, user.getState().getCode());
		assertEquals(true, clientManager.associate(new AssociationKeyWithEmail(key.getKey(), email)));
		assertEquals(CODE_USER_ROLE_ADMIN, user.getRole().getCode());
		assertEquals(CODE_USER_STATE_ACTIVE, user.getState().getCode());
		assertEquals(client.getId(), user.getClient().getId());
		assertNull(oneOrNull(find(AssociationKey.class, "hash", key.getHash())));
	}
	
	public void testAssociateNewUserOk() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		AssociationKey key = clientManager.generateKey(client.getId());
		assertNotNull(oneOrNull(find(AssociationKey.class, "hash", key.getHash())));
		String email = "some@mail";
		assertEquals(0, find(User.class, "email", email).size());
		assertEquals(true, clientManager.associate(new AssociationKeyWithEmail(key.getKey(), email)));
		User user = exactlyOne(find(User.class, "email", email));
		assertEquals(CODE_USER_ROLE_ADMIN, user.getRole().getCode());
		assertEquals(CODE_USER_STATE_ACTIVE, user.getState().getCode());
		assertEquals(client.getId(), user.getClient().getId());
		assertNull(oneOrNull(find(AssociationKey.class, "hash", key.getHash())));
	}
	
	public void testGetByEmaiAndActivateChangeState() {
		String email = "qqq18231789@mail";
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		User user = clientManager.createUser(client.getId(), new UserCreate(email));
		assertEquals(CODE_USER_STATE_INVITED, user.getState().getCode());
		User foundUser = clientManager.getUserByEmailAndActivate(email);
		assertEquals(CODE_USER_STATE_ACTIVE, foundUser.getState().getCode());
		assertEquals(user.getId(), foundUser.getId());
	}
	
	public void testGetUserException() {
		Client client = findClient(Initializer.CONTRAGENT_NAME_MTS);
		User user = exactlyOne(find(User.class, "phone", Initializer.CLIENT_MTS_USER_PHONE));
		clientManager.getUser(client.getId(), user.getId());
		setExpectedUserGetItemException();
		clientManager.getUser(client.getId() + 1, user.getId());
	}

}
